package ru.adhoc.truealarm.notification;

import android.app.ActivityManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import ru.adhoc.truealarm.R;
import ru.adhoc.truealarm.activities.MainActivity;
import ru.adhoc.truealarm.db.DbHelper;
import ru.adhoc.truealarm.services.NotificationService;
import ru.adhoc.truealarm.time.TimeHelper;
import ru.adhoc.truealarm.utils.Constants;
import ru.adhoc.truealarm.utils.Util;

import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.text.format.DateFormat;
import android.util.Log;

import java.util.Calendar;
import java.util.List;

/**
 * Created by Малюгин Сергей on 25.07.13.
 */
public class NfHelper {

    private static NfHelper instance;
    private static final int NOTIFY_ID = 9999;
    Context context;
    DbHelper DbHelper;

    public NfHelper(Context c) {
        context = c;
    }

    public static NfHelper getInstance(Context ctx){

        if (instance == null){
            instance = new NfHelper(ctx);
        }
        return instance;
    }

    //Возвращает количество активных будильников
    private int getCountActiveAlarms(){
        DbHelper = DbHelper.getInstance(context);

        int count = 0;
        SQLiteDatabase db = DbHelper.getWritableDatabase();

        try {
            count = DbHelper.getCountActiveAlarms(db);
        }
        finally {
            if (db != null)
                db.close();
        }

        return count;
    }

    public void showNotificationService(long resetTime, boolean postponed){

        //Пробуем каждый раз останавливать сервис перез его запуском
        if(notificationServiceIsRunning())
            cancelShowNotificationService(true);

        Intent myIntent = new Intent(context, NotificationService.class);
        myIntent.putExtra("resetTime", resetTime);
        myIntent.putExtra("postponed", postponed);
        context.startService(myIntent);
    }

    public void cancelShowNotificationService(boolean isShow){

        context.stopService(new Intent(context, NotificationService.class));

        if(getCountActiveAlarms() == 0){
            NotificationManager nm =
                    (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
            nm.cancel(NOTIFY_ID);
        }
        else if(!isShow){
            showNotificationService(0L, false);
        }
    }

    private boolean notificationServiceIsRunning(){

        ActivityManager am = (ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> rs = am.getRunningServices(50);
        for (int i=0; i<rs.size(); i++) {
            ActivityManager.RunningServiceInfo rsi = rs.get(i);
            if(NotificationService.class.getName().equalsIgnoreCase(rsi.service.getClassName())){
                return true;
            }
        }
        return false;
    }

}
